package br.edu.unipampa.client;

import br.edu.unipampa.encryption.Caesar;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 *
 * @author aLeX
 */
public class Client {

    private final String nameClient;
    private final int port;
    private final String SERVER = "localhost";
    private int numMsg;
    private String mensagem;
    private Socket cliente;
    private BufferedReader noUsuario;
    private DataOutputStream preparaParaServidor;

    public Client(String nameClient, int port) throws IOException {
        this.nameClient = nameClient.toUpperCase();
        this.port = port;
        this.numMsg = 0;
    }

    public String getNameClient() {
        return this.nameClient;
    }

    public int getNumMsg() {
        return this.numMsg;
    }

    public void enviarMensagemServidor() throws IOException {
        this.cliente = new Socket(this.SERVER, this.port);
        this.noUsuario = new BufferedReader(new InputStreamReader(System.in));
        this.preparaParaServidor = new DataOutputStream(this.cliente.getOutputStream());
        System.out.println("Digite sua mensagem do " + this.nameClient + ":");
        this.numMsg++;
        this.mensagem = this.noUsuario.readLine() + "<" + this.numMsg + ">";
        this.mensagem = this.mensagem.toUpperCase();

        Caesar cipher = new Caesar(); //Cria um objeto com a Cifra de César
        String msgAux = cipher.encrypt(this.nameClient + ": " + this.mensagem);
        this.preparaParaServidor.writeBytes(msgAux + "\n");
    }

    public void encerrarConexão() throws IOException {
        this.cliente.close();
    }

}
